﻿using System;
using System.Windows.Forms;

using Moe.ECamel.Common.Entities;
using Moe.ECamel.Client.Controllers;
using Moe.ECamel.Client.WinForms.Common;
using Moe.ECamel.Client.WinForms.Forms;
using Moe.ECamel.Common.Security;

namespace Moe.ECamel.Client.WinForms.Controls
{
	public partial class RentalListControl : ECamelUserControl
	{
		private RentalsController _controller;

		public RentalListControl()
		{
			InitializeComponent();
		}

		public void Initialize(RentalsController controller)
		{
			this._controller = controller;
			this.ListBindingSource.DataSource = _controller.List;
			ApplyPermissions();
		}

		private void ApplyPermissions()
		{
			EntityViewToolStripButton.Available = EntityViewToolStripMenuItem.Available = ECamelHelper.IsPermitted(Operations.RentalView);
			EntityEditToolStripButton.Available = EntityEditToolStripMenuItem.Available = ECamelHelper.IsPermitted(Operations.RentalReturn);
			PayFineToolStripButton.Available = PayFineToolStripMenuItem.Available = ECamelHelper.IsPermitted(Operations.RentalPayFine);
		}

		public void RefreshView()
		{
			// Refresh entity list:
			this._controller.RefreshList();
			// Refresh binding:
			this.ListBindingSource.CurrencyManager.Refresh();
			// Refresh menus:
			RefreshMenus();

			GetActiveRentalsToolStripMenuItem.Checked = this._controller.ActiveOnlyFilter;
			GetAllRentalsToolStripMenuItem.Checked = !GetActiveRentalsToolStripMenuItem.Checked;
		}

		private void RefreshMenus()
		{
			// Get current entity:
			var entity = (Rental)ListBindingSource.Current;
			// Refresh available controls:
			bool hasValue = (entity != null);
			EntityViewToolStripButton.Enabled = EntityViewToolStripMenuItem.Enabled = hasValue;
			EntityEditToolStripButton.Enabled = EntityEditToolStripMenuItem.Enabled = hasValue;
			PayFineToolStripButton.Enabled = PayFineToolStripMenuItem.Enabled = hasValue;
		}

		private void ChangeActiveOnlyFilter(bool activeOnly)
		{
			this._controller.ActiveOnlyFilter = activeOnly;
			RefreshView();
		}

		private void ViewEntity()
		{
			// Get current entity:
			var entity = (Rental)ListBindingSource.Current;
			if (entity != null)
			{
				using (var form = new RentalDetailsForm(_controller.NewViewController(entity)))
				{
					form.ShowDialog();
				}
			}
		}
		private void ReturnCurrentRental()
		{
			// Get current entity (if no entity return):
			var entity = (Rental)ListBindingSource.Current;
			if (entity == null)
				return;

			// Validate rental can be returned:
			if (entity.ActualReturnDate != null)
			{
				ECamelHelper.ShowMessage(MessageIds.RentalReturnInvalidAlreadyRetuend);
				return;
			}

			// Ask user for return date - if user cancelled abort:
			var returnDate = DatePickerForm.ShowSelectSingleDateDialog(entity.RentalDate, entity.RentalDate, null);
			if (returnDate == null)
				return;

			var response = _controller.ReturnRental(entity, returnDate.Value);
			if (response.Error != null)
				ECamelHelper.ShowMessage(response.Error);
			else
			{
				if (response.Fine != 0)
				{
					var result = ECamelHelper.ShowMessage(MessageIds.RentalReturnFineForLateReturn, response.Fine);
					if (result == DialogResult.Yes)
						PayFine(entity, returnDate.Value);
				}
				RefreshView();					
			}
		}
		private void PayFine()
		{
			// Get current entity (if no entity return):
			var entity = (Rental)ListBindingSource.Current;
			if (entity == null)
				return;

			// Validate fine can be payed:
			var error = entity.ValidateFinePayment();
			if (error != null)
			{
				ECamelHelper.ShowMessage(error);
				return;				
			}

			// Ask user for payment date - if user cancelled abort:
			var paymentDate = DatePickerForm.ShowSelectSingleDateDialog(entity.RentalDate, entity.RentalDate, null);
			if (paymentDate == null)
				return;

			PayFine(entity, paymentDate.Value);
			RefreshView();
		}
		private void PayFine(Rental rental, DateTime paymentDay)
		{
			var error = _controller.PayFine(rental, paymentDay);
			if (error != null)
				ECamelHelper.ShowMessage(error);
		}


		private void EntityViewToolStripButton_Click(object sender, EventArgs e)
		{
			ViewEntity();
		}
		private void EntityEditToolStripButton_Click(object sender, EventArgs e)
		{
			ReturnCurrentRental();
		}
		private void PayFineToolStripButton_Click(object sender, EventArgs e)
		{
			PayFine();
		}

		private void EntityViewToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ViewEntity();
		}
		private void EntityEditToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ReturnCurrentRental();
		}
		private void PayFineToolStripMenuItem_Click(object sender, EventArgs e)
		{
			PayFine();
		}

		private void ListDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			ViewEntity();
		}

		private void GetActiveRentalsToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ChangeActiveOnlyFilter(true);
		}
		private void GetAllRentalsToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ChangeActiveOnlyFilter(false);
		}
	}
}